package com.itheima.article.controller;

import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.pojo.ApAuthor;
import com.itheima.article.service.ApArticleService;
import com.itheima.article.service.ApAuthorService;
import com.itheima.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ApiController {

    @Autowired
    private ApAuthorService apAuthorService;

    @Autowired
    private ApArticleService apArticleService;

    @GetMapping("/api/apAuthor/{apUserId}/{wmUserId}")
    public ResultVo<ApAuthor> getByApUserIdWmUserId(
            @PathVariable(name = "apUserId")Long apUserId,
            @PathVariable(name = "wmUserId")Long wmUserId){
        ApAuthor apauthor = apAuthorService.getByApUserIdWmUserId(apUserId,wmUserId);
        return ResultVo.ok(apauthor);
    }


    @PostMapping("/api/apAuthor/add")
    public ResultVo add(@RequestBody ApAuthor apAuthor){
       apAuthorService.save(apAuthor);
       return ResultVo.ok();
    }

    /**
     * 同步文章微服务到文章
     * @param dto
     * @return
     */
    @PostMapping("/api/apArticle/saveArticleInfo")
    public ResultVo saveArticleInfo(@RequestBody ArticleInfoDto dto){
        apArticleService.saveArticleInfo(dto);
        return ResultVo.ok();
    }

    /**
     * 通过文章id查询作者名称
     * @param articleId
     * @return
     */
    @GetMapping("/api/apArticle/getAuthorName/{articleid}")
    public ResultVo<String> getAuthorName(@PathVariable(value = "articleid")Long articleId){
        ApArticle apArticle = apArticleService.getById(articleId);
        Object authorName = apArticle.getAuthorName();
        return ResultVo.ok(authorName);
    }

}
